import java.util.List;
import java.util.Map;

import models.Student;
import play.Application;
import play.GlobalSettings;
import play.libs.Yaml;

import com.avaje.ebean.Ebean;

public class Global extends GlobalSettings {
	// Yaml file must be in /conf/ folder
	private static String	fixtureFile	= "test-data.yml";

	@Override
	public void onStart(Application app) {
		InitialData.insert(app);
	}

	static class InitialData {
		public static void insert(Application app) {
			if (Ebean.find(Student.class).findRowCount() == 0) {

				// Check whether database is empty
				// If empty, then insert fixtures from yml file
				if (Student.find.findRowCount() == 0) {
					Map<String, List<Object>> tableMap = (Map<String, List<Object>>) Yaml
							.load(Global.fixtureFile);

					for (Map.Entry<String, List<Object>> tableEntry : tableMap
							.entrySet()) {
						Ebean.save(tableEntry.getValue());
						play.Logger.info("loaded "
								+ tableEntry.getValue().size() + " "
								+ tableEntry.getKey() + " from '" + fixtureFile
								+ "' into the database");
					}
				}
			}
		}
	}

}
